< previous page page_205 next page >

Page 205
application may suddenly vanish, or other bizarre behavior may appear in your application.
The sample code below shows you how to correctly handle the DEVMODE structure in the DocumentProperties call:
7017a6ead0e3c4111b47a554df321e9f.gif
Dim hPrinter&
Dim res&
Dim dmIn As DEVMODE
Dim dmOut As DEVMODE
Dim DevmodeLength As Long
We allocate two dynamic byte buffers to hold the DEVMODE structure and the printer's private data:
7017a6ead0e3c4111b47a554df321e9f.gif
Dim dmInBuf() As Byte
Dim dmOutBuf() As Byte

' Non-zero on success
res = OpenPrinter(Printer.DeviceName, hPrinter, 0)
If res = 0 Then
   lblStatus.Caption = "Printer could not be opened"
   Exit Sub
End If
The DocumentProperties function returns the length of the necessary buffer if you set the fMode parameter to 0. This length is used to dimension the two byte arrays:
' Get the length of the DEVMODE structure for this printer
   DevmodeLength = DocumentProperties(hwnd, hPrinter, _
   Printer.DeviceName, VarPtr(dmOut), VarPtr(dmIn), 0)
   lblComment.Caption = "The DEVMODE structure for this printer is " _
   & DevmodeLength & " bytes. " & _
   "The standard DEVMODE structure is " & Len(dmOut) & " bytes."
   ReDim dmInBuf(DevmodeLength)
   ReDim dmOutBuf(DevmodeLength)

Pass a pointer to the start of the byte arrays as the DEVMODE parameters.

   ' Load the dmOutBuf buffer with the data
   res = DocumentProperties(hwnd, hPrinter, Printer.DeviceName, _
   VarPtr(dmOutBuf(0)), VarPtr(dmInBuf(0)), DM_OUT_BUFFER Or DM_IN_PROMPT)

 
< previous page page_205 next page >